/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import data.UserData;
import data.PendidikanData;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import database.PendidikanDB;

/**
 *
 * @author Muhammad Dony
 */
public class PendidikanUserServlet extends HttpServlet {
    UserData listUser;
    PendidikanDB pendidikandb;
    HttpSession session;
    private String action;
    PendidikanData listPendidikan;
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
    
    
    
    protected void halamanPendidikan(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        session = request.getSession();
        UserData user = (UserData) session.getAttribute("userMasuk");
        List listpendidikan = new PendidikanDB().getpendidikanById(user.getID());
        request.setAttribute("listpendidikan", listpendidikan);        
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/User/profil/view_pendidikan.jsp");
        rd.forward(request, response);
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // login validation start
        session = request.getSession();
        Object ud = session.getAttribute("userMasuk");
        UserData udata = (UserData) ud;
        if (udata != null) {
           // if (udata.isLogin()) { // if login
                action = request.getParameter("action");
                if (request.getParameter("action") == null) {
                    try {
                        halamanPendidikan(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(PendidikanUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(PendidikanUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (action.equals("add")) {
                    halamanTambahPendidikan(request, response);
                } else if (action.equals("edit")) {
                    try {
                        halamanEditPendidikan(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(PendidikanUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(PendidikanUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (action.equals("delete")) {
                    try {
                        deletePendidikan(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(PendidikanUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(PendidikanUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                else { // if not login
                RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/User/login_user.jsp");
                rd.forward(request, response);
            }
        } else {
            RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/User/login_user.jsp");
            rd.forward(request, response);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // get session
        session = request.getSession();
        // mengambil object sesiUser dari session
        Object ud = session.getAttribute("userMasuk");
        UserData udata = (UserData) ud;
        // mengambil object dengan parameter action dari request
        action = request.getParameter("action");

        if (request.getParameter("action") == null) {
                    try {
                        halamanPendidikan(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(PendidikanUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(PendidikanUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (action.equals("Tambah")) {
                    try {
                        tambahPendidikan(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(PendidikanUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(PendidikanUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (action.equals("Edit")) {
                    try {
                        editPendidikan(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(PendidikanUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(PendidikanUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                
                }
    }
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
    public void halamanTambahPendidikan(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setAttribute("title", "Tambah Pendidikan");
        request.setAttribute("buttonName", "Tambah");
        request.setAttribute("listPendidikan", new PendidikanData());
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/User/profil/add_pendidikan.jsp");
        rd.forward(request, response);
    }

    public void halamanEditPendidikan(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        request.setAttribute("title", "Edit Pendidikan");
        request.setAttribute("buttonName", "Edit");
        String ID  = request.getParameter("id");
        int no  = Integer.parseInt(request.getParameter("no"));
        List listpendidikan = new PendidikanDB().getpendidikanByxx(ID, no);
        PendidikanData dataPendidikan = (PendidikanData) listpendidikan.get(0);
        request.setAttribute("dataPendidikan", dataPendidikan);
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/User/profil/add_pendidikan.jsp");
        rd.forward(request, response);
    }

    public void tambahPendidikan(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        // membuat object listUser
        listPendidikan = new PendidikanData();
        // set listUser
        listPendidikan.setID(request.getParameter("ID"));
        listPendidikan.setInstitusi(request.getParameter("institusi"));
        listPendidikan.setJurusan(request.getParameter("jurusan"));
        listPendidikan.setMasuk(request.getParameter("masuk"));
        listPendidikan.setTamat(request.getParameter("tamat"));
        // memasukkannya dalam database
        pendidikandb = new PendidikanDB();
        pendidikandb.insertPendidikan(listPendidikan);
        // redirect ke halaman pelaporan
        halamanPendidikan(request, response);
    }

    public void editPendidikan(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        // membuat object listUser
        listPendidikan = new PendidikanData();
        // set listUser
        listPendidikan.setNomor(Integer.parseInt(request.getParameter("no")));
        listPendidikan.setID(request.getParameter("ID"));
        listPendidikan.setInstitusi(request.getParameter("institusi"));
        listPendidikan.setJurusan(request.getParameter("jurusan"));
        listPendidikan.setMasuk(request.getParameter("masuk"));
        listPendidikan.setTamat(request.getParameter("tamat"));
        // memasukkannya dalam database
        pendidikandb = new PendidikanDB();
        pendidikandb.updatePendidikan(listPendidikan);
        // redirect ke halaman pelaporan
        halamanPendidikan(request, response);
    }

    public void deletePendidikan(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        String ID = request.getParameter("id");
        int no  = Integer.parseInt(request.getParameter("no"));
        pendidikandb = new PendidikanDB();
        pendidikandb.deletePendidikan(ID, no);
        halamanPendidikan(request, response);
    }
    
}
